package com.meals.system.api.seller;


import com.meals.system.api.seller.param.SystemSellerLoginParam;
import com.meals.system.api.seller.param.SystemSellerRegisterParam;
import com.meals.system.api.seller.param.SystemSellerUpdateParam;
import com.meals.system.api.seller.vo.WhitesMealSellerVO;
import com.meals.system.api.user.param.SystemUserRegisterParam;
import com.meals.system.api.user.param.SystemUserUpdateParam;
import com.meals.system.api.user.vo.WhitesMealUserVO;
import com.meals.system.common.Constants;
import com.meals.system.common.ServiceResultEnum;
import com.meals.system.config.annotation.TokenToSeller;
import com.meals.system.config.annotation.TokenToSystemUser;
import com.meals.system.entity.SystemSeller;
import com.meals.system.entity.SystemUser;
import com.meals.system.service.WhitesMealSellerService;
import com.meals.system.util.BeanUtil;
import com.meals.system.util.NumberUtil;
import com.meals.system.util.Result;
import com.meals.system.util.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * Description:
 * Parm:
 * return:
 * Author: Chen
 * Date: 2021/12/10
 */
@RestController
@Api(value = "v1", tags = "2-1.白餐商家操作相关接口")
@RequestMapping("/seller-api/v1")
public class WhitesMealSellerAPI {

    @Resource
    private WhitesMealSellerService whitesMealSellerService;

    private static final Logger logger = LoggerFactory.getLogger(WhitesMealSellerAPI.class);

    @PostMapping("/seller/login")
    @ApiOperation(value = "登录接口", notes = "返回token")
    public Result<String> login(@RequestBody @Valid SystemSellerLoginParam systemSellerLoginParam){
        if (!NumberUtil.isPhone(systemSellerLoginParam.getLoginName())){
            return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_NAME_IS_NOT_PHONE.getResult());
        }
        String loginResult = whitesMealSellerService.login(systemSellerLoginParam.getLoginName(), systemSellerLoginParam.getPasswordMd5());

        logger.info("login api,loginName={}, loginResult={}", systemSellerLoginParam.getLoginName(), loginResult);

        // 登录成功
        if (StringUtils.hasLength(loginResult) && loginResult.length() == Constants.TOKEN_LENGTH){
            Result result = ResultGenerator.genSuccessResult();
            result.setData(loginResult);
            return result;
        }

        // 登录失败
        return ResultGenerator.genFailResult(loginResult);

    }

    @PostMapping("/seller/logout")
    @ApiOperation(value = "登出接口",notes = "清除token")
    public Result<String> logout(@TokenToSeller SystemSeller loginSystemSeller){
        Boolean logoOutResult = whitesMealSellerService.logout(loginSystemSeller.getSellerId());

        logger.info("logout api,loginSystemUser={}", loginSystemSeller.getSellerId());

        // 登出成功
        if (logoOutResult){
            return ResultGenerator.genSuccessResult();
        }
        // 登出失败
        return ResultGenerator.genFailResult("登出失败");
    }

    @PostMapping("/seller/register")
    @ApiOperation(value = "用户注册", notes = "")
    public Result register(@RequestBody @Valid SystemSellerRegisterParam systemSellerRegisterParam){
        if (!NumberUtil.isPhone(systemSellerRegisterParam.getLoginName())){
            return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_NAME_IS_NOT_PHONE.getResult());
        }
        String registerResult = whitesMealSellerService.register(systemSellerRegisterParam.getLoginName(), systemSellerRegisterParam.getPassword());

        logger.info("register api, loginName={}, loginResult={}", systemSellerRegisterParam.getLoginName(),systemSellerRegisterParam.getPassword());

        // 注册成功
        if (ServiceResultEnum.SUCCESS.getResult().equals(registerResult)){
            return ResultGenerator.genSuccessResult();
        }
        // 注册失败
        return ResultGenerator.genFailResult(registerResult);

    }

    @PutMapping("/seller/info")
    @ApiOperation(value = "修改用户信息", notes = "")
    public Result updateInfo(@RequestBody @ApiParam("用户信息") SystemSellerUpdateParam systemSellerUpdateParam, @TokenToSeller SystemSeller loginSystemSeller){
        Boolean flag = whitesMealSellerService.updateSellerInfo(systemSellerUpdateParam, loginSystemSeller.getSellerId());
        if (flag){
            // 修改成功
            Result result = ResultGenerator.genSuccessResult();
            return result;
        } else {
            //修改失败
            Result result = ResultGenerator.genFailResult("修改失败");
            return result;
        }

    }

    @GetMapping("/seller/info")
    @ApiOperation(value = "获取用户信息", notes = "")
    public Result<WhitesMealSellerVO> getSystemSellerDetail(@TokenToSeller SystemSeller loginSystemSeller){
        //已经登录
        WhitesMealSellerVO whitesMealSellerVO = new WhitesMealSellerVO();
        BeanUtil.copyProperties(loginSystemSeller, whitesMealSellerVO);
        return ResultGenerator.genSuccessResult(whitesMealSellerVO);
    }


}
